package com.tjsiqi.service;

import com.tjsiqi.Vo.*;
import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.beans.PageResult;
import com.tjsiqi.dao.JblSendTruckOUTMapper;
import com.tjsiqi.dto.SendCountDto;
import com.tjsiqi.exception.ParamException;
import com.tjsiqi.model.JblSendTruckOUT;
import com.tjsiqi.model.JblSendTruckOUTEXP;
import com.tjsiqi.model.WxhntRWDEXP;
import com.tjsiqi.param.SendCountParam;
import com.tjsiqi.util.DataHandleUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SysSendOrderService {
    @Autowired
    private JblSendTruckOUTMapper jblSendTruckOUTMapper;

    //  查询发货单数据
    public PageResult<JblSendTruckOUT> searchPageList(SendCountParam sendCountParam, PageQuery page) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        int count = jblSendTruckOUTMapper.getSendOrder(sendCountDto);
         if (count > 0){
            List<JblSendTruckOUT> sendJblSendTruckOUTVoList = jblSendTruckOUTMapper.getPageList(sendCountDto, page);
            return PageResult.<JblSendTruckOUT>builder().total(count).data(sendJblSendTruckOUTVoList).build();
        }
        return PageResult.<JblSendTruckOUT>builder().build();
    }

//  封装查询数据，默认赋初值
    private static SendCountDto getSendCDto(SendCountParam sendCountParam){
        SendCountDto sendCountDto = new SendCountDto();
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNotBlank(sendCountParam.getFromTime())) {
                sendCountDto.setFromTime(dateFormat.parse(sendCountParam.getFromTime()+" 00:00:00"));
            }else{
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, 0);
                // 分
                cal.set(Calendar.MINUTE, 0);
                // 秒
                cal.set(Calendar.SECOND, 0);
                // 毫秒
                cal.set(Calendar.MILLISECOND, 0);
//                cal.add(Calendar.DATE, -7);
                //设置开始时间
                sendCountDto.setFromTime(cal.getTime());
            }
            //设置结束时间
            if (StringUtils.isNotBlank(sendCountParam.getToTime())) {
                sendCountDto.setToTime(dateFormat.parse(sendCountParam.getToTime()+" 23:59:59"));
            }else{
                Calendar calTo = Calendar.getInstance();
                calTo.set(Calendar.HOUR_OF_DAY, 23);
                // 分
                calTo.set(Calendar.MINUTE, 59);
                // 秒
                calTo.set(Calendar.SECOND, 59);
                // 毫秒
                calTo.set(Calendar.MILLISECOND, 0);
                sendCountDto.setToTime(calTo.getTime());
            }
        } catch (Exception e) {
            throw new ParamException("传入的日期格式有问题，正确格式为：yyyy-MM-dd HH:mm:ss");
        }

        sendCountDto.setJbzCode(sendCountParam.getJbzCode());

        return sendCountDto;
    }

    // 导出发货单
    public List<JblSendTruckOUTEXP> exportByParam(SendCountParam sendCountParam) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<JblSendTruckOUTEXP> sendOrderList = jblSendTruckOUTMapper.exportByParam(sendCountDto);

        return sendOrderList;
    }
    //  移除发货单
    public int removeByParam(SendCountParam sendCountParam) {
        int isRemove = jblSendTruckOUTMapper.removeByParam(sendCountParam);
        return isRemove;
    }
}
